package com.it.bigdata.parse.impl;

import com.it.bigdata.domain.TieZi;
import com.it.bigdata.parse.Parser;
import com.it.bigdata.schedual.Schedual;
import com.it.bigdata.schedual.impl.BaiDuTieBaSchedualForQueue;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;

public class BaiDuTieBaParser implements Parser {
    private HtmlCleaner htmlCleaner = new HtmlCleaner();
    private Schedual schedual = BaiDuTieBaSchedualForQueue.getBdQueue();

    public void parsePageList(String html) {
        //解析html,取出页面地址,把其加入到资源调度器中（Queue）
        TagNode tagNode = htmlCleaner.clean(html);
        try {
            Object[] objects = tagNode.evaluateXPath("//a[@class='j_th_tit ']/@href");

            for (Object object : objects) {
                schedual.push(object.toString());
            }
        } catch (XPatherException e) {
            e.printStackTrace();
        }

    }

    public TieZi parserCurrentToTieZi(String html) {
        TagNode tagNode = htmlCleaner.clean(html);
        TieZi tieZi = new TieZi();
        try {
            //所有发帖人
            Object[] objects1 = tagNode.evaluateXPath("//*[@id=\"j_p_postlist\"]/div[1]/div[1]/ul/li[3]/a/text()");
            if (objects1 != null && objects1.length > 0) {
                String author = objects1[0].toString();
                tieZi.setAuthor(author);
            }


            //System.out.println(author);
            Object[] objects2 = tagNode.evaluateXPath("//*[@id=\"j_core_title_wrap\"]/h3/text()");
            if (objects2 != null && objects2.length > 0) {
                String title = objects2[0].toString();
                tieZi.setTitle(title);
            }
            Object[] objects3 = tagNode.evaluateXPath("//img[@class=\"BDE_Image\"]/@src");
            for (Object o : objects3) {
                tieZi.getImages().add(o.toString());
            }
            System.out.println(tieZi);
        } catch (XPatherException e) {
            e.printStackTrace();
        }
        return null;

    }
}
